﻿using System;
using System.Linq;
using System.Xml;
using NUnit.Framework;

namespace Twitter.Tests
{
	[TestFixture]
	public class PayloadReaderFixture : TestBase
	{
		[ExpectedException(typeof(ArgumentNullException))]
		[Test]
		public void ShouldThrowIfNullReaderMessages()
		{
			PayloadReader.ReadMessages(null).ToList();
		}

		[ExpectedException(typeof(ArgumentNullException))]
		[Test]
		public void ShouldThrowIfNullReaderUsers()
		{
			PayloadReader.ReadUserScreenNames(null).ToList();
		}

		[ExpectedException(typeof(ArgumentNullException))]
		[Test]
		public void ShouldThrowIfNullReaderStatuses()
		{
			PayloadReader.ReadStatuses(null).ToList();
		}

		[Test]
		public void ShouldReadMessages()
		{
			using (XmlReader reader = XmlReader.Create("direct_messages.xml"))
			{
				var messages = PayloadReader.ReadMessages(reader).ToList();

				Assert.AreEqual(2, messages.Count);

				Assert.AreEqual("17074402", messages[0].Id);
				Assert.AreEqual("fnbtest", messages[0].Sender.ScreenName);
				Assert.AreEqual("fnbtesta", messages[0].Recipient.ScreenName);
				Assert.IsNotNull(messages[0].Created);
				Assert.AreEqual(
					DateTime.Parse("12/1/2007 9:43:47 GMT").ToUniversalTime(), 
					messages[0].Created);
				Assert.AreEqual("Buenos Aires", messages[0].Sender.Location);
				Assert.AreEqual("A test user", messages[0].Sender.Description);
				Assert.AreEqual("http://static.twitter.com/images/default_profile_normal.png", messages[0].Sender.ProfileImageUrl);
				Assert.AreEqual("", messages[0].Sender.Url);
				Assert.AreEqual(false, messages[0].Sender.Protected);

				Assert.AreEqual("17074382", messages[1].Id);
				Assert.AreEqual("fnbtestc", messages[1].Sender.ScreenName);
				Assert.AreEqual("fnbtesta", messages[1].Recipient.ScreenName);
			}
		}

		[Test]
		public void ShouldReadUsers()
		{
			using (XmlReader reader = XmlReader.Create("friends.xml"))
			{
				var friends = PayloadReader.ReadUserScreenNames(reader).ToList();
				Assert.AreEqual(2, friends.Count);
				Assert.AreEqual("edjez", friends[0]);
				Assert.AreEqual("kzu", friends[1]);
			}

			using (XmlReader reader = XmlReader.Create("followers.xml"))
			{
				var followers = PayloadReader.ReadUserScreenNames(reader).ToList();
				Assert.AreEqual(3, followers.Count());
				Assert.AreEqual("edjez", followers[0]);
				Assert.AreEqual("waj", followers[1]);
				Assert.AreEqual("kzu", followers[2]);
			}
		}

		[Test]
		public void ShouldReadStatuses()
		{
			using (XmlReader reader = XmlReader.Create("public_timeline.xml"))
			{
				var statuses = PayloadReader.ReadStatuses(reader).ToList();
				Assert.AreEqual(4, statuses.Count);
				Assert.AreEqual("461139692", statuses[0].Id);
				Assert.AreEqual("461139682", statuses[1].Id);
				Assert.AreEqual("The Palinode's making what he calls his First-Twitter-of-the-Day Face. It apparently requires his arms be held out at an awkward angle.", statuses[1].Text);
				Assert.AreEqual("tkf", statuses[2].User.ScreenName);
				Assert.AreEqual("Chris Abraham", statuses[3].User.Name);
				Assert.IsNotNull(statuses[0].Created);
				Assert.AreEqual(
					DateTime.Parse("Sat, 1 Dec 2007 20:43:42 GMT").ToUniversalTime(),
					statuses[0].Created);
			}
		}
	}
}
